using System;
using System.Collections.Generic;
using System.Text;

using Danser_Control.Managers;
using Danser_Entity.Entities;

namespace Danser_AJAXEnabledWebApplication.DataSourceManagers
{
    /// <summary>
    /// Contains Data Source support methods for the users entities
    /// </summary>
    public class UsersDataSourceManager
    {
        #region Memebers:

        /// <summary>
        /// Local list. holds a copy of the table's records in DB,
        /// + may be 1 more empty record representing a new row in datagrid
        /// </summary>
        private IList<User> _users;

        /// <summary>
        /// Control layer interface to access the DB
        /// </summary>
        private IUserManager manager;

        #endregion

        #region C'tor:

        /// <summary>
        /// creates a UsersDataSourceManager instance
        /// </summary>
        public UsersDataSourceManager()
        {
            //Get users from proper control interface
            manager = ManagerFactory.UserManager();
            _users = manager.GetUsers((IList<String>)null);
        }

        #endregion

        #region Query Methods:

        /// <summary>
        /// CREATE method implementation
        /// </summary>
        /// <param name="userID">user id to add</param>
        /// <param name="name">name - will be enriched by control layer</param>
        /// <param name="permission">permission level for the user</param>
        public void AddUser(string userID, string name, PermissionLevel permission)
        {
            //Do nothing. 
            //This is a "virtual" call that will cause data binding of the datagrid.
            //When data binding occurs, it'll be notified that there's a new row
            //to display. see GetUsers() below.
        }

        /// <summary>
        /// SELECT method implementation
        /// </summary>
        /// <param name="newRow">set to true if new row in the UI</param>
        /// <param name="filter">filter used in WHERE clause</param>
        /// <returns>list of users data</returns>
        public IList<User> GetUsers(bool newRow, String filter)
        {
            //show the new row when grid performs data binding
            if (newRow) //add new row on copied list
            {
                User newUser = new User();
                //insert only to the local list. DB doesn't know this row
                _users.Insert(0, newUser);
            }
            else //get users according to filter
            {
                _users = manager.GetUsers(filter);
            }
            return _users;
        }

        /// <summary>
        /// UPDATE method implementation
        /// </summary>
        /// <param name="userID">user id to modify</param>
        /// <param name="name">the name (read only - not changed)</param>
        /// <param name="permission">modified permision level</param>
        /// <param name="newRow">indicates if execution is for a new user or existing one</param>
        /// <exception cref="Danser_Control.Exceptions.DanserAuthorizationException"></exception>
        public void ModifyUser(string userID, string name, PermissionLevel permission, bool newRow)
        {
            //if activated for a new row
            if (newRow)
            {
                //call the interface's add method
                manager.AddUser(userID, permission);
            }
            else
            {
                //send request to DB
                manager.ModifyPermission(userID, permission);
            }
           
        }

        /// <summary>
        /// DELETE method implementation
        /// </summary>
        /// <param name="userId">user id to remove</param>
        /// <param name="newRow">set to true if new row in the UI</param>
        public void RemoveUser(string userId, bool newRow)
        {
            if (!newRow)
            {
                //send request to DB
                manager.RemoveUser(userId);
            }

        }

        #endregion
    }
}
